package networkSimulation.simulation.social.framework

import networkSimulation.framework.{Edge, Graph, Node}
import util.Random

/**
 * This class represents a person.
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
class Person(name:String) extends Node(name){

  /**
   * Custom simulation implementation.
   */
  @Override
  override def simulate[N<%Node, E<%Edge[N]](graph: Graph[N, E]) {
    val neighbours = graph.getNeighbours(this.asInstanceOf[N])                           // get neighbours
    if (neighbours.length != 0){                                                         // are there neighbours?
      val neighbour = neighbours.apply(new Random().nextInt(neighbours.length))        // pick a random neighbour
      val neighboursFromNeighbour = graph.getNeighbours(neighbour)                       // get neighbours from neighbour
      if (neighboursFromNeighbour.length > 0){
        for (bro : Person <- neighboursFromNeighbour.map(_.asInstanceOf[Person]).toList){  // for each [above] ...
          if (!neighbours.contains(bro) && !bro.equals(this)){                                                 // check if they are not already 'friends'

            if(graph.getEdge(this.asInstanceOf[N],neighbour).apply(0).getClass() == graph.getEdge(neighbour.asInstanceOf[N],bro.asInstanceOf[N]).apply(0).getClass) {
                 if (new Random().nextInt(2) == 1) {                                       // 50% chance to befriend this bro
                    if (graph.getEdge(this.asInstanceOf[N],neighbour).apply(0).isInstanceOf[School[Person]])
                      graph.addEdge((new School[Person](this,bro.asInstanceOf[Person])).asInstanceOf[E])
                   if (graph.getEdge(this.asInstanceOf[N],neighbour).apply(0).isInstanceOf[FootBall[Person]])
                      graph.addEdge((new FootBall[Person](this,bro.asInstanceOf[Person])).asInstanceOf[E])
                   if (graph.getEdge(this.asInstanceOf[N],neighbour).apply(0).isInstanceOf[GayCommunity[Person]])
                      graph.addEdge((new GayCommunity[Person](this,bro.asInstanceOf[Person])).asInstanceOf[E])
                 }
            }// check if 'friendship'-type is ok
          }
        }
      }
    }
  }

  /**
   * Custom color.
   */
  @Override
  override def getColor() = "lightblue"

}